package com.fivefu.core.skyeye.event.services.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fivefu.base.common.utils.date.DateUtils;
import com.fivefu.core.skyeye.event.entity.DbAiEvent;
import com.fivefu.core.skyeye.event.entity.DbAiEventFailed;
import com.fivefu.core.skyeye.event.mapper.DbAiEventFailedMapper;
import com.fivefu.core.skyeye.event.services.DbAiEventFailedService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 事件错误日志表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-08-24 17:25:24
 */
@Service
public class DbAiEventFailedServiceImpl extends ServiceImpl<DbAiEventFailedMapper, DbAiEventFailed> implements DbAiEventFailedService {

    /**
     * 新增事件错误日志
     * @param failed
     */
    @Override
    public void insertEventFailed(DbAiEventFailed failed){
        LocalDateTime time = LocalDateTime.now();
        failed.setUpdatedTime(time);
        failed.setCreatedTime(time);
        failed.setIsDelete(0);
        baseMapper.insert(failed);
    }

    @Override
    public Page<Map<String, Object>> queryAiEventFailedList(Integer page, Integer limit, String cameraid, String sceneid) {
        Page<Map<String,Object>> resultPage = new Page<>();
        List<Map<String,Object>> resultList = new ArrayList<>();
        List<DbAiEventFailed> list = new ArrayList<>();
        //判断是否传了page和limit
        if (null != page && null != limit) {
            Page<DbAiEventFailed> iPage = new Page<>(page,limit);
            iPage = baseMapper.getAiEventFailedList(iPage,cameraid,sceneid);
            list = iPage.getRecords();
            resultPage.setTotal(iPage.getTotal());
        } else {
            list = baseMapper.getAiEventFailedList(cameraid,sceneid);
            resultPage.setTotal(list.size());
        }
        list.forEach(dbAiEventFailed -> {
            Map<String,Object> resultMap = formatAiEventFailed(dbAiEventFailed);
            resultList.add(resultMap);
        });
        resultPage.setRecords(resultList);
        return resultPage;
    }
    /**
     * 格式化事件错误日志
     * @param dbAiEventFailed
     * @return
     */
    private Map<String, Object> formatAiEventFailed(DbAiEventFailed dbAiEventFailed) {
        Map<String,Object> map = JSON.parseObject(JSON.toJSONString(dbAiEventFailed, SerializerFeature.WriteMapNullValue), Map.class);

        //创建时间
        if (null != dbAiEventFailed.getCreatedTime()) {
            String time = DateUtils.format(dbAiEventFailed.getCreatedTime(),DateUtils.DEFAULT_DATETIME_PATTERN);
            map.put("createdTime",time);
        }
        //更新时间
        if (null != dbAiEventFailed.getUpdatedTime()) {
            String time = DateUtils.format(dbAiEventFailed.getUpdatedTime(),DateUtils.DEFAULT_DATETIME_PATTERN);
            map.put("updatedTime",time);
        }
        //报错时间
        if (null != dbAiEventFailed.getFailedTime()) {
            String time = DateUtils.format(dbAiEventFailed.getFailedTime(),DateUtils.DEFAULT_DATETIME_PATTERN);
            map.put("failedTime",time);
        }

        return map;
    }
}
